package gui;

import gui.dialogs.dialogPanels;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TreeMap;
import java.util.Vector;

import javax.swing.*;

import dataTypes.Film;
import dataTypes.Musor;
import dataTypes.Terem;

public class TimeLine {
	private JLabel []ora, teremnevek;
	private JPanel fo,termek, ido;
	private JPanel []teremsavok;
	private static JScrollPane timeline;
	private final int magassag=60;
	private final int oraszelesseg=120;
	private DBConnect dbc;
	private TreeMap<Integer,Film> filmlista;
	private Vector<Terem> teremlista;
	private static java.sql.Date aktdatum;
	class Musorelem{
		private Film film;
		private JButton gomb;
		private Musor musor;
		public Musorelem(Film film, Musor musor){
			this.film=film;
			this.musor=musor;
			
			int ertek = musor.ido.getHours()*60+musor.ido.getMinutes();
			this.gomb = new JButton(film.cim);
			this.gomb.setBounds(ertek/60*oraszelesseg, 0, film.idotartam/60*oraszelesseg, magassag);
			this.gomb.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					modosit();
				}
				
			});
		}
		public JButton getGomb(){
			return this.gomb;
		}
		public void modosit(){
			gui.dialogs.musorModositas.CreateMusorModositoDialog(musor, film);
		}
	}
	public TimeLine(){
		dbc = new DBConnect();
		teremlista=dbc.getTermek();
		fo = new JPanel(null);		
		ido=new JPanel(null);
		termek = new JPanel(null);
		ora=new JLabel[24];
		filmlista=dbc.getFilmek();
		for(int i=0;i<10;i++){
			ora[i]=new JLabel("0"+i+":00");
			ora[i].setBounds(i*oraszelesseg,0,40,30);
			ido.add(ora[i]);
		}
		for(int i=10;i<24;i++){
			this.ora[i]= new JLabel(i+":00");
			this.ora[i].setBounds(i*oraszelesseg,0,40,30);
			this.ido.add(ora[i]);
		}
		
		teremnevek= new JLabel[teremlista.size()];
		for(int i=0;i<teremlista.size();i++){
			teremnevek[i]=new JLabel(teremlista.get(i).teremnev);
			teremnevek[i].setBounds(0,i*magassag,120,magassag);
			termek.add(teremnevek[i]);
		}
		teremsavok = new JPanel[teremnevek.length];
		for(int i=0;i<teremsavok.length;i++){
			teremsavok[i]= new JPanel(null);
			teremsavok[i].setBounds(0,i*magassag,oraszelesseg*24,magassag);
			/*if(i%2==0)
			teremsavok[i].setBackground(Color.blue);
			else teremsavok[i].setBackground(Color.red);*/
			fo.add(teremsavok[i]);
		}
		termek.setPreferredSize(new Dimension(120,teremlista.size()*magassag));
		ido.setBounds(0, 0, 24*oraszelesseg, 30);
		ido.setPreferredSize(new Dimension(24*oraszelesseg,30));
		
		fo.setBounds(0,0,24*oraszelesseg,teremlista.size()*magassag);
		fo.setPreferredSize(new Dimension(24*oraszelesseg,teremlista.size()*magassag));
		timeline = new JScrollPane(fo,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		timeline.setColumnHeaderView(ido);
		timeline.setRowHeaderView(termek);
		timeline.setBounds(0, 40, 600,460);
		
		
	}
	public void GenerateMusor(java.sql.Date d){
		aktdatum = d;
		for(int i=0;i<teremsavok.length;i++){
			if(teremsavok[i].getComponentCount()!=0){
				teremsavok[i].removeAll();
				teremsavok[i].updateUI();
			}
		}
		
		
		for (int i=0;i<teremlista.size();i++){
			Vector<Musor> musorok =dbc.getMusor(teremlista.get(i).Teremid, d);
			for(int j=0;j<musorok.size();j++){
				Musorelem m = new Musorelem(filmlista.get(musorok.get(j).filmid),musorok.get(j));
				teremsavok[i].add(m.getGomb());
			}
			
		}
		
	
		
	}
	public void UpdateMusor(){
		GenerateMusor(aktdatum);
	}
	public JScrollPane getTimeLine(){
	
		return timeline;
	}
}
